<template>
  >> {{title}}&nbsp;&nbsp;<el-button @click="goback()">返回</el-button>
  <lay-json-schema-form :model="form" :schema="schema1" ></lay-json-schema-form>
  <lay-button type="primary" @click="submit1">提交</lay-button>
</template>

<script>
import {ref, reactive, toRefs, onMounted, readonly} from 'vue';
import { layer } from '@layui/layer-vue'

import {getModelsColume, getModelsData, editModels} from '@/api/model'
import md5 from 'js-md5';
import { useRouter } from 'vue-router'

export default {
  props: {
    pagename: String,
    modelname: String,
    title: String,
    url: String,
    orderfield: String,
    orderasc: String,
    selectstr: String,
    tableColumns: String,
    id: Number
  },
  methods: {
    goback() {
      this.$router.go(-1)
    },
  },
  setup (props){
    const router = useRouter();
    const id = props.id;
    const modelname = props.modelname;
    const pagename = props.pagename;

    var select = props.selectstr
    var columns = []
    if((select != undefined) && (select != "*")){
      columns = select.split(",")
    }else{
      select = "*"
    }
    columns.push("id")

    const state = reactive({
      form: new Object(),
      schema1: {
        tablename: {
          label: '编辑表',
          type: 'rate'
        },
      },
      columns: columns
    })

    const getTableName = () => {
      getModelsData(modelname,id).then(res => {
        state.form["tablename"] = "编辑";
        let obj = res.data[0];
        for(let key in obj){
          if(state.columns.length > 0) {
            if (state.columns.includes(key)) {
              state.form[key] = obj[key]
            }
          }else{
            state.form[key] = obj[key]
          }
        }
      })
      getModelsColume(modelname).then(res => {
        for (let i = 0; i < res.data.length; i++) {
          let obj = res.data[i];
          if(state.columns.length > 0){
            if(state.columns.includes(obj.Field)){
              if(obj.Field == "id"){
                state.schema1[obj.Field] =  {
                  label: obj.Comment,
                  type: 'input',
                  props: {
                    type: 'text',
                    placeholder: '请输入'+obj.Comment,
                    readonly: readonly
                  }
                }
              }else{
                state.schema1[obj.Field] =  {
                  label: obj.Comment,
                  type: 'input',
                  props: {
                    type: 'text',
                    placeholder: '请输入'+obj.Comment,
                  }
                }
              }
            }
          }else{
            state.schema1[obj.Field] =  {
              label: obj.Comment,
              type: 'input',
              props: {
                type: 'text',
                placeholder: '请输入'+obj.Comment,
              }
            }
          }
        }
      })
    }

    /**
     const form = reactive({
  code: 'HS123456',
  codename: '路途集团'
})
     */

    const submit1 = () => {
      if("password" in state.form){
        state.form["password"] = md5(state.form["password"])
      }
      editModels(modelname,state.form).then(res => {
        layer.msg('编辑成功', { time: 2000 });
        router.push("/manage/pagetable/"+pagename.replace("-edit",""))
      })

    }

    onMounted(getTableName)

    return {
      ...toRefs(state),
      submit1,
      id,
      modelname,
      pagename
    }
  }
}
</script>
